package com.zxl.live.tools.anim;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

/**
 * Created by Administrator on 2016/8/14.
 */

public class AnimatorHelper {

    /**
     * 单向移动
     * @param target
     * @param propertyName  "translationX" "translationY"
     * @param translation
     * @param listener
     */
    public static void showTranslationRockAnimator(View target, String propertyName, int translation, Animator.AnimatorListener listener) {
        ObjectAnimator transXAnimator = ObjectAnimator.ofFloat(target, propertyName, 0, -translation);
        transXAnimator.setInterpolator(new AccelerateInterpolator(3f));
        if (null != listener) {
            transXAnimator.addListener(listener);
        }
        transXAnimator.setDuration(500);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(target, "alpha", 1f, 0f);
        alphaAnimator.setInterpolator(new AccelerateInterpolator(3f));
        alphaAnimator.setDuration(500);

        ObjectAnimator transXAnimator1 = ObjectAnimator.ofFloat(target, propertyName, translation, 0);
        transXAnimator1.setInterpolator(new DecelerateInterpolator(3f));
        transXAnimator1.setDuration(350);

        ObjectAnimator alphaAnimator1 = ObjectAnimator.ofFloat(target, "alpha", 0f, 1f);
        alphaAnimator1.setInterpolator(new DecelerateInterpolator(3f));
        alphaAnimator1.setDuration(350);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(alphaAnimator).with(transXAnimator);
        animatorSet.play(alphaAnimator1).after(transXAnimator);
        animatorSet.play(transXAnimator1).after(transXAnimator);

        animatorSet.start();
    }

    /**
     * 左右晃动
     * @param target
     * @param listener
     */
    public static void showLeftAndRightRockAnimator(View target, Animator.AnimatorListener listener) {
        ObjectAnimator transAnim1 = ObjectAnimator.ofFloat(target, "translationX", 0f, -24f);
        transAnim1.setDuration(80);
        ObjectAnimator transAnim2 = ObjectAnimator.ofFloat(target, "translationX", -24f, 20f);
        transAnim2.setDuration(80);
        ObjectAnimator transAnim3 = ObjectAnimator.ofFloat(target, "translationX", 20f, -24f);
        transAnim3.setDuration(80);
        ObjectAnimator transAnim4 = ObjectAnimator.ofFloat(target, "translationX", -24f, 0f);
        transAnim4.setDuration(80);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(transAnim2).after(transAnim1);
        animatorSet.play(transAnim3).after(transAnim2);
        animatorSet.play(transAnim4).after(transAnim3);
        if (null != listener) {
            animatorSet.addListener(listener);
        }
        animatorSet.start();

    }
}
